package com.tsinkai.ettp.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class HttpRequest {
	
	public static String doGet(String HttpUrl){
		HttpURLConnection connection = null;
		InputStream is = null;
		BufferedReader br = null;
		String result = null;
		
		try {
			//创建远程URL连接对象
			URL url = new URL(HttpUrl);
			//通过URL对象打开一个连接
			connection = (HttpURLConnection)url.openConnection();
			//设置请求方式
			connection.setRequestMethod("GET");
			//设置连接主机服务器的超时时间
			connection.setConnectTimeout(15000);
			//设置读取远程返回数据时间
			connection.setReadTimeout(60000);
			//发送请求
			connection.connect();
			if(connection.getResponseCode() == 200){
				//通过connection获取输入流
				 is = connection.getInputStream();
				//封装输入流，并设置编码
				 br = new BufferedReader(new InputStreamReader(is, "utf-8"));
				StringBuffer sbf = new StringBuffer();
				String temp = null;
				while((temp = br.readLine())!=null){
					sbf.append(temp);
					sbf.append("\r\n");
				}
				result = sbf.toString();
			}
			if(connection.getResponseCode() == 201){
				System.out.println("201");
				
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(br != null){
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(is != null){
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			connection.disconnect();
		}
		return result;
	}
}
